home *** CD-ROM | disk | FTP | other *** search
- #include <Arrays_real.h>
- #include <CurvPlot.h>
-
- // forward declarations:
- void scheme (Vec(real)& up1, Vec(real)& u, Vec(real)& um1, real tstop, real C);
- void setInitialCondition (Vec(real)& u0, Vec(real)& um1, real C);
- void plotSolution (Vec(real)& u, CurvPlotFile& plotfile, real t, real C);
-
- int main (int nargs, const char** args)
- {
- initDIFFPACK (nargs, args);
- cout << "Give number of intervals in (0,1): ";
- int i; cin >> i;
- int n = i+1; // number of points;
- Vec(real) up1 (n); // u at time level k+1
- Vec(real) u (n); // u at time level k
- Vec(real) um1 (n); // u at time level k-1
- cout << "Give Courant number: ";
- real C; cin >> C;
- cout << "Compute u(x,t) for t <= tstop, where tstop = ";
- real tstop; cin >> tstop;
-
- setInitialCondition (u, um1, C); // initialization
- scheme (up1, u, um1, tstop, C); // finite difference scheme
- return 0;
- }
-
- void scheme (Vec(real)& up1, Vec(real)& u, Vec(real)& um1, real tstop, real C)
- {
- int n = u.size(); // length of the vector u (no of grid points)
- real h = 1.0/(n-1); // length of grid intervals
- real dt = C*h; // time step, assumes UNIT wave VELOCITY!
- real t = 0; // time
- CurvPlotFile plotfile(CaseName); // "databank" (file) with all the plots
-
- plotSolution (u, plotfile, t, C); // plot the initial string displacement
- int i; // loop counter
- int step_no = 0; // current step number
-
- while (t <= tstop)
- {
- t += dt; step_no++;
- for (i = 2; i <= n-1; i++)
- up1(i) = 2*u(i) - um1(i) + sqr(C) * (u(i+1) - 2*u(i) + u(i-1));
- up1(1) = 0; up1(n) = 0; // boundary condition at x=0 and x=1
- plotSolution (up1, plotfile, t, C);
- um1 = u; u = up1; // update data structures for next step
- if (step_no % 100 == 0) // write a message for every 100th step:
- cout << oform("time step %4d: u(x,t=%6.3f) is computed.\n",step_no,t)
- << flush; // the flush forces immediate output
- }
- }
-
- void setInitialCondition (Vec(real)& u0, Vec(real)& um1, real C)
- {
- int n = u0.size(); // length of the vector u
- real x; // coordinate of a grid point
- real h = 1.0/(n-1); // length of grid intervals
- real umax = 0.05; // max string displacement
-
- for (int i = 1; i <= n; i++) // set the initial displacement of the string
- {
- x = (i-1)*h;
- if (x < 0.7)
- u0(i) = (umax/0.7) * x;
- else
- u0(i) = (umax/0.3) * (1 - x);
- }
- for (i = 2; i <= n-1; i++) // set the help variable um1:
- um1(i) = u0(i) + 0.5*sqr(C) * (u0(i+1) - 2*u0(i) + u0(i-1));
- um1(1) = 0; um1(n) = 0; // dummy values, will not be used in the scheme
- }
-
- void plotSolution (Vec(real)& u, CurvPlotFile& plotfile, real t, real C)
- {
- int n = u.size(); // the number of unknowns
- real h = 1.0/(n-1); // length of grid intervals
- CurvPlot plot (plotfile);
- plot.initPair ("displacement", // plot title
- oform("u(x,%g)",t), // name of function
- "x", // name of independent variable
- oform("C=%g, h=%g",C,h)); // comment
- for (int i = 1; i <= n; i++)
- plot.addPair (h*(i-1) /* x-value */, u(i) /* function value */);
- plot.finish();
- }
-